/* 
 * frm-layout.js
 * @since 1.0 <2015-6-8> SoChishun Added.
 */

/**
 * 历史菜单对象
 * @since 1.0 <2015-6-8> SoChishun Added.
 */
var nav_history = {
    container_id: '#nav_history',
    storage_id: 'nav_history',
    /**
     * 初始化
     * @since 1.0 <2015-6-8> SoChishun Added.
     */
    init: function () {
        nav_history.load();
        // 关闭菜单历史
        $(nav_history.container_id).delegate('i', 'click', function () {
            nav_history.remove($(this).prev());
        });
        // 设置当前菜单历史
        $(nav_history.container_id).delegate('a', 'click', function () {
            nav_history.current($(this));
        });
    },
    /**
     * 加载历史菜单
     * @since 1.0 <2015-6-8> SoChishun Added.
     */
    load: function () {
        var sdata = window.localStorage.getItem(nav_history.storage_id);
        if (sdata) {
            var data = $.parseJSON(sdata);
            var opts;
            for (var i = 0; i < data.items.length; i++) {
                $(nav_history.container_id).append('<li' + (data.cur.id == data.items[i].id ? ' class="cur"' : '') + '><a href="' + data.items[i].url + '" data-id="' + data.items[i].id + '">' + data.items[i].text + '</a><i></i></li>');
            }
        }
    },
    /**
     * 添加到历史菜单中
     * @since 1.0 <2015-6-8> SoChishun Added.
     */
    add: function ($a) {
        // nav_history={items:[{id:"",text"",url:""}...],cur:0};
        var id = $a.data('id');
        if (!id) {
            return;
        }
        var sdata = window.localStorage.getItem(nav_history.storage_id);
        var item = {id: id, text: $a.text(), url: $a.attr('href')};
        if (sdata) {
            var data = $.parseJSON(sdata);
            var has_id = false;
            for (var i = 0; i < data.items.length; i++) {
                if (id == data.items[i].id) {
                    has_id = true;
                    break;
                }
            }
            if (!has_id) {
                data.items.push(item);
                if (data.items.length > 10) {
                    data.items.shift();
                }
            }
            data.cur = item;
        } else {
            var data = {items: [item], cur: item};
        }
        window.localStorage.setItem(nav_history.storage_id, JSON.stringify(data));
    },
    /**
     * 更新当前历史菜单
     * @since 1.0 <2015-6-8> SoChishun Added.
     */
    current: function ($a) {
        var id = $a.data('id');
        if (!id) {
            return;
        }
        var sdata = window.localStorage.getItem(nav_history.storage_id);
        if (!sdata) {
            return;
        }
        var data = $.parseJSON(sdata);
        data.cur = {id: id, text: $a.text(), url: $a.attr('href')};
        window.localStorage.setItem(nav_history.storage_id, JSON.stringify(data));
    },
    /**
     * 删除历史菜单
     * @since 1.0 <2015-6-8> SoChishun Added.
     */
    remove: function ($a) {
        var id = $a.data('id');
        if (!id) {
            return;
        }
        var sdata = window.localStorage.getItem(nav_history.storage_id);
        if (!sdata) {
            return;
        }
        var data = $.parseJSON(sdata);
        var n = data.items.length;
        if (n < 2) {
            return; // 至少保留一个
        }
        // 1.是cur,则焦点后移,cur=next(),当前移除,跳转
        // 2.不是cur,则焦点保持,当前移除,不跳转
        var url = '';
        for (var i = 0; i < n; i++) {
            if (id == data.items[i].id) {
                if (id == data.cur.id) {
                    data.cur = i >= n - 1 ? data.items[i - 1] : data.items[i + 1];
                    url = data.cur.url;
                }
                $(nav_history.container_id).find('a[data-id="' + id + '"]').parent().remove();
                data.items.splice(i, 1);
                break;
            }
        }
        window.localStorage.setItem(nav_history.storage_id, JSON.stringify(data));
        if (url) {
            location.href = url;
        }

    }
};
/**
 * 动态生成导航菜单
 * @param {JSON} data
 * @since 1.0 <2015-6-6> SoChishun Added.
 */
function create_nav(data) {
    if (!data) {
        return;
    }
    var submm = '';
    for (var i = 0; i < data.length; i++) {
        submm += '<div id="nav_sub_' + data[i].i + '" class="main-nav-sub">';
        var sub2 = data[i].sub;
        if (sub2) {
            for (var i2 = 0; i2 < sub2.length; i2++) {
                var sub3 = sub2[i2].sub;
                if (sub3) {
                    for (var i3 = 0; i3 < sub3.length; i3++) {
                        submm += '<div><a href="/index.php/XCall_5_0' + sub3[i3].l + '" data-id="' + sub3[i3].i + '">' + sub3[i3].n + '</a></div>';
                    }
                }
            }
        }
        submm += '</div>';
    }
    $('#main_nav_submms').html(submm);
    for (var i = 0; i < data.length; i++) {
        $('#nav_' + data[i].i).menubutton({menu: '#nav_sub_' + data[i].i});
    }
}
